#!/usr/bin/env ruby
# frozen_string_literal: true

require 'json'
require 'zlib'

# Load query analyzers
require_relative File.expand_path('database/query_analyzers.rb', __dir__)

source = ENV['CI_MERGE_REQUEST_SOURCE_BRANCH_SHA']
target = ENV['CI_MERGE_REQUEST_TARGET_BRANCH_SHA']
log_path = ENV['RSPEC_AUTO_EXPLAIN_LOG_PATH']
logs_path = File.dirname(log_path)

exit(0) unless Dir.exist?(logs_path)

fingerprints = Set.new
jobs = Set.new
query_analyzers = Database::QueryAnalyzers.new

JOB_NAME = %r{^(.*)\.\d+\.[^.]+\.ndjson\.gz$}

Zlib::GzipWriter.open(log_path) do |log|
  Dir[File.join(logs_path, '*.gz')].reject { |p| p == log_path }.each do |file|
    job_name = File.basename(file)[JOB_NAME, 1]
    Zlib::GzipReader.open(file) do |gz|
      gz.each_line do |line|
        query = JSON.parse(line)
        fingerprint = query['fingerprint']

        next unless fingerprints.add?(fingerprint)

        query_analyzers.analyze(query)

        jobs << job_name
        query['job_name'] = job_name
        log.puts(JSON.generate(query))
      end
    end

    File.delete(file)
  end
end

query_analyzers.save!

warn("auto_explain log contains #{fingerprints.size} entries from: #{jobs.to_a.sort.join(', ')}")
warn("auto_explain comparison of #{target} to #{source}") if source && target
